import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class JdbcUpdate {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改同学的名字：");
        String name1 = scanner.next();
        System.out.println("将该同学的名字改为：");
        String name2 = scanner.next();

        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/practice?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("111111");

        Connection connection = dataSource.getConnection();

        String sql = "update student set name = ? where name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, name2);
        statement.setString(2, name1);
        System.out.println("sql = " + statement);

        int n = statement.executeUpdate();
        System.out.println("n = " + n);

        statement.close();
        connection.close();
    }
}
